import java.util.*;

public class Text {
    //56
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,(v1, v2) -> v1[0] - v2[0]);
        int[][] ret = new int[intervals.length][2];
        int index = -1;
        for(int[] interval:intervals) {
            if(index == -1 || ret[index][1] < interval[0]) {
                ret[++index] = interval;
            }else{
                ret[index][1] = Math.max(ret[index][1],interval[1]);
            }
        }
        return Arrays.copyOf(ret, index + 1);
    }
}
